﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.ValidationService;
using A2DFramework.ValidationService.RuleValidators;
using System.ComponentModel.DataAnnotations;

namespace ValidationTestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            A2DFramework.Starter.Init();
            A2DFramework.RuleEngine.Starter.Init();
            A2DFramework.BusinessExceptionService.Starter.Init();

            User user = new User();
            user.FirstName = "";
            user.CustomField = "aaron";
            Console.WriteLine("=========无场景验证==========");
            foreach (var error in user.IsValid())
            {
                string msg = string.Format("key: {0}, msg: {1}", error.Key, error.Message);
                Console.WriteLine(msg);
            }
            Console.WriteLine("==========有场景验证 [场景1]=========");
            foreach (var error in user.IsValid("场景1"))
            {
                string msg = string.Format("key: {0}, msg: {1}", error.Key, error.Message);
                Console.WriteLine(msg);
            }

            Console.ReadKey();
        }
    }



    public class User
    {
        [Required]
        [RuleMobileAttribute("场景1")]
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Mobile { get; set; }

        [RuleCustomValidation("场景1", typeof(UserValidators), "ValidateCustomField")]
        public string CustomField { get; set; }
    }

    public class UserValidators
    {
        public static ValidationResult ValidateCustomField(string fieldValue)
        {
            bool isValid=false;

            if (fieldValue == "aaron")
                isValid = true;

            if (isValid)
            {
              return ValidationResult.Success;
            }
            else
            {
              return new ValidationResult("The selected sales person is not available for this customer.");
            }
      }
    }
}
